//
//  CZVisitorView.swift
//  CZWeibo
//
//  Created by 周振兰 on 16/5/10.
//  Copyright © 2016年 周振兰. All rights reserved.
//

import UIKit
//1.定义协议
@objc protocol CZVisitorViewDelegate:NSObjectProtocol
{
    //2.定义协议方法  注册点击按钮后的方法
    func visitorViewDidClickRegister()
    
    //登录按钮被点击后的代理方法
    func  visitorViewDidClickLogin()
}

class CZVisitorView: UIView {
    var name : String
    //3.定义代理属性
    weak var delegate : CZVisitorViewDelegate?
    //设置访客视图
    func setupVistorInfo(imageName: String,message: String)
    {
        //将转轮图片替换
        iconView.image = UIImage(named: imageName)
        
        //设置messageLable 
        messagelable.text = message
        
        //隐藏小房子
        homeView.hidden = true
        //隐藏遮盖
        coverView.hidden = true
        
        
    }
    
    
    
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
   override init(frame: CGRect) {
    self.name = ""
        super.init(frame: frame)
    prepareUI()
    
    }
    
    func startRotationAnimation ()
    {
        //创建核心动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = M_PI * 2
        anim.duration = 20
        anim.repeatCount = MAXFLOAT
        

        // 核心动画完成的时候不要移除
        anim.removedOnCompletion = false
        //将核心动画添加到图层上面去
        iconView.layer.addAnimation(anim, forKey: nil)
    }
    //设置UI
    private func prepareUI()
    {
        // 设置背景颜色
        //        self.backgroundColor = UIColor(red: 232 / 255.0, green: 232 / 255.0, blue: 232 / 255.0, alpha: 1)
        self.backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
        //将子控件添加
        addSubview(iconView)
        addSubview(coverView)
        addSubview(homeView)
        addSubview(registButton)
        addSubview(longinButton)
        addSubview(messagelable)
        

        iconView.translatesAutoresizingMaskIntoConstraints = false
        
        coverView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        registButton.translatesAutoresizingMaskIntoConstraints = false
        longinButton.translatesAutoresizingMaskIntoConstraints = false
        messagelable.translatesAutoresizingMaskIntoConstraints = false
        
        //转轮的约束  约束最好只添加一次 因为   override init(frame: CGRect)这个方法值加载一次，所以
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
        
        // 遮盖
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
    self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem:longinButton , attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        //小房子的约束
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: .Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
        
         //消息的lable
        
        addConstraint(NSLayoutConstraint(item: messagelable, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 240))
        
        // CenterX 和父控件CenterX

        addConstraint(NSLayoutConstraint(item: messagelable, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //top和转轮的底部
        addConstraint(NSLayoutConstraint(item: messagelable, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        //注册按钮  顶部和lable的底部
        addConstraint(NSLayoutConstraint(item: registButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messagelable, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        ///左对齐与menssagelable
        addConstraint(NSLayoutConstraint(item:registButton, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: messagelable, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        
        //宽度
        addConstraint(NSLayoutConstraint(item: registButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        //高度
        addConstraint(NSLayoutConstraint(item: registButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        //登录按钮
        addConstraint(NSLayoutConstraint(item: longinButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messagelable, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        //右对齐
        addConstraint(NSLayoutConstraint(item: longinButton, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: messagelable, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        //宽度
        addConstraint(NSLayoutConstraint(item: longinButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        //高度
        addConstraint(NSLayoutConstraint(item: longinButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
       
    
    }
    // MARK: - 按钮点击事件
    // 按钮点击事件方法是系统来调用, 当把按钮点击事件private后系统就找不到这个方法了
    // @objc: 让系统可以找到我们的方法
    
  @objc private  func didClikResgist ()
    {
         // view不能Modal出控制器来,需要将事件传递给控制器
        //调用代理方法
        delegate?.visitorViewDidClickRegister()
    }
    
    @objc private func didlonginButton ()
    {
        //调用代理方法
        delegate?.visitorViewDidClickLogin()
    }
    
   // MARK: - K 懒加载 所有控件
      //转轮
        private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    /// 遮盖视图
    private lazy var coverView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //小房子
    private lazy var homeView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //消息文字
    private  lazy var messagelable: UILabel =
    {
        let lable = UILabel()
        lable.text = "关注一些人,看看有什么惊喜!"
        lable.textColor = UIColor.blackColor()
        lable.numberOfLines = 0
        lable.sizeToFit()
        lable.textColor = UIColor.blackColor()
        lable.font = UIFont.systemFontOfSize(16)
        lable.textAlignment =  NSTextAlignment.Center
        return lable
    }()
    
    
    ///注册按钮
    private lazy var registButton :UIButton =
    {
       let button = UIButton()
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        button.setTitle("注册", forState: UIControlState.Normal)
        button.addTarget(self, action: "didClikResgist", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    
    //登录按钮
    private lazy var longinButton :UIButton =
    {
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        button.setTitle("登录", forState: UIControlState.Normal)
        
        //点击事件
        button.addTarget(self, action: "didlonginButton", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    
}
















